<template>
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="560px"
    v-loading="loading"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form
      :model="checkitemModel"
      :rules="formRules"
      ref="roleForm"
      label-width="80px"
      label-position="right"
      size="small"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目编码" prop="code">
            <el-input v-model="checkitemModel.code" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="name">
            <el-input v-model="checkitemModel.name" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-select v-model="checkitemModel.sex">
              <el-option label="不限" value="0"></el-option>
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="适用年龄">
            <el-input v-model="checkitemModel.age" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-select v-model="checkitemModel.type">
              <el-option label="检查" value="1"></el-option>
              <el-option label="检验" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="价格">
            <el-input v-model="checkitemModel.price" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目说明">
            <el-input v-model="checkitemModel.remark" type="textarea"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="注意事项">
            <el-input v-model="checkitemModel.attention" type="textarea"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('roleForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { updateCheckiem, saveCheckitem } from '@/api/checkitem'

export default {
  name: "checkitem-edit",
  data() {
    return {
      loading: false,
      checkitemModel: {
        code: "",
        name: "",
        sex:"",
        age:"",
        price:"",
        type:"",
        remark:"",
        attention:"",
      },
      dialogProps: {
        visible: false,
        action: "",
        title: "",
      },
      formRules: {
        code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
        name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveCheckitem();
          } else {
            this.doUpdateCheckitem();
          }
        } else {
          return false;
        }
      });
    },
    doUpdateCheckitem() {
      this.loading = true;
      updateCheckiem(this.checkitemModel.id, this.checkitemModel)
        .then((response) => {
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    doSaveCheckitem() {
      this.loading = true;
      saveCheckitem(this.checkitemModel)
        .then((response) => {
          this.loading = false;
          this.dialogProps.visible = false;
          this.$emit("save-finished");
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    handleDialogClose() {
      this.dialogProps.visible = false;
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },
  },
  mounted: function () {
    this.$nextTick(() => {
      this.$on("openEditCheckitemDialog", function (checkitem) {
        this.checkitemModel = checkitem;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "修改检查项";
        this.dialogProps.visible = true;
      });
      this.$on("openAddCheckitemDialog", function () {
        this.dialogProps.action = "add";
        this.dialogProps.title = "添加检查项";
        this.checkitemModel = {};
        this.dialogProps.visible = true;
      });
    });
  },
};
</script>

